<template>
  <div class="setting-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleFn"
              >新增角色</el-button>
            </el-row>
            <!-- 使用 Table 组件实现用户角色的渲染 -->
            <el-table border style="width: 100%" :data="rolesList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column label="角色名" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row.id)">分配权限</el-button>
                  <el-dialog title="分配权限" width="50%" :visible.sync="dialogVisible">
                    <assign-permission v-if="dialogVisible" :role-id="rowId" @close="dialogVisible=false" />
                  </el-dialog>
                  <!-- 弹框 -->
                  <el-dialog
                    :title="isEdit?'编辑':'添加'"
                    :visible.sync="dialogShow"
                    width="50%"
                    :close-on-click-modal="false"
                    :close-on-press-escape="false"
                    @close="closeDialog"
                  >
                    <el-form ref="form" :model="roleForm" label-width="80px" :rules="rules">
                      <el-form-item label="角色名称" prop="name">
                        <el-input v-model="roleForm.name" />
                      </el-form-item>

                      <el-form-item label="角色描述" prop="description">
                        <el-input v-model="roleForm.description" />
                      </el-form-item>
                      <el-form-item>
                        <el-button type="primary" @click="roleSubmit">确认</el-button>
                        <el-button @click="roleCancel">取消</el-button>
                      </el-form-item>
                    </el-form>
                  </el-dialog>
                  <el-button size="small" type="primary" @click="editRoles(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[2, 5, 8, 10]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyList.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyList.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyList.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyList.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoles, getCompany, addRole, getRolesDetail, updateRoles, delRoles } from '@/api/setting'
import { mapGetters } from 'vuex'
import assignPermission from './assignPermission.vue'
export default {
  name: 'Setting',
  components: { assignPermission },
  data() {
    return {
      activeName: 'first',
      query: {
        page: 1, // 当前页面
        pagesize: 2 // 页面显示的条数
      },
      rolesList: [], // 角色列表
      total: 0, // 角色数据总条数
      dialogShow: false,
      roleForm: {
        name: '', // 姓名
        description: '' // 描述

      },
      companyList: {},
      rules: {
        name: [{ required: true, trigger: 'blur', message: '角色名称必填' }],
        description: [{ required: true, trigger: 'blur', message: '角色描述必填' }]
      },
      isEdit: false,
      dialogVisible: false, // 分配权限
      rowId: ''
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    // 调用获取角色方法
    this.getRoles()
    // 调用获取企业的方法
    this.getCompany()
  },
  methods: {
    // 弹框关闭前的回调
    closeDialog() {
      // 使用v-if并不会将数组清空,只是将组件销毁,数据依旧存在对象里
      this.roleForm = {
        name: '', // 姓名
        description: '' // 描述
      }
      this.$refs.form.resetFields()
    },
    // 点击新增的按钮
    addRoleFn() {
      // 弹框显示
      this.dialogShow = true
      // 编辑的标识
      this.isEdit = false
    },
    // 编辑的方法
    async updateRoles() {
      const res = await updateRoles({ companyId: this.companyId, ...this.roleForm })
      // console.log(res)
      // 提示
      this.$message.success(res.message)
      // 渲染
      this.getRoles()
      // 关闭弹窗
      this.dialogShow = false
    },
    // 点击新增确认按钮
    roleSubmit() {
      this.isEdit ? this.updateRoles() : this.addRole()
    },
    // 点击新增取消按钮
    roleCancel() { this.dialogShow = false },
    // 添加角色的api
    addRole() {
      // 兜底验证
      this.$refs.form.validate(async valid => {
        if (!this.isEdit) {
          if (!valid) return
          const res = await addRole(this.roleForm).catch(err => err)
          console.log(res)
          // 提示
          this.$message.success(res.message)
          // 渲染
          this.getRoles()
          // 关闭弹窗
          this.dialogShow = false
        }
      })
    },
    // 根据id查询企业api
    async getCompany() {
      const res = await getCompany(this.companyId)
      this.companyList = res.data
    },
    // 获取所有角色列表api
    async getRoles() {
      const res = await getRoles(this.query)
      // console.log('2222', res)
      this.rolesList = res.data.rows
      this.total = res.data.total
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(pageSize) {
      this.query.pagesize = pageSize
      this.getRoles()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(current) {
      this.query.page = current
      this.getRoles()
    },

    // 分配设置角色
    setRoles(id) {
      this.rowId = id
      this.dialogVisible = true
    },

    // 编辑角色
    async editRoles(obj) {
      this.isEdit = true
      const res = await getRolesDetail(obj.id)
      console.log(res)
      this.roleForm = res.data
      this.dialogShow = true
    },

    // 删除角色
    async delRoles(obj) {
      // console.log(obj)
      const delRes = await this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(delRes)
      if (delRes === 'cancel') {
        this.$message('取消删除')
      } else {
        const res = await delRoles(obj.id)
        // 如果删除最后为1条,删除之后请求前一页的数据
        if (this.rolesList.length === 1) {
          this.query.page--
        }

        if (this.query.page <= 0) {
          this.query.page = 1
        }
        // 提示
        this.$message.success(res.message)
        // 渲染
        this.getRoles()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
.form.el-form {
    padding: 0 60px 0 30px;
}
</style>
